package com.hfy.kandian.entity;

import com.baomidou.mybatisplus.annotation.IdType;

import java.io.*;
import java.util.*;

import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableField;

import com.hfy.kandian.dao.representation.BookRepresentation;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

/**
 * <p>
 *
 * </p>
 *
 * @author HAN XIAOYANG
 * @since 2020-12-30
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "Book对象", description = "")
public class Book implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "书籍ID")
    @TableId(value = "book_id", type = IdType.AUTO)
    private Integer bookId;

    @ApiModelProperty(value = "作者")
    private Integer writerId;

    @ApiModelProperty(value = "书名")
    private String bookName;

    @ApiModelProperty(value = "封面")
    private String bookPic;

    @ApiModelProperty(value = "标签")
    private String bookTag;

    @ApiModelProperty(value = "简介")
    private String bookIntroduction;

    @ApiModelProperty(value = "内容")
    private String bookContent;

    @ApiModelProperty(value = "票数")
    private Integer bookVote;

    @ApiModelProperty(value = "审核状态:0：未通过；1：通过")
    private Integer auditStatus;

    @ApiModelProperty(value = "0为非VIP书籍，1为VIP书籍")
    private Integer bookVip;

    @ApiModelProperty(value = "0为未被删除；1为被删除")
    @TableLogic
    private Integer deleted;

    @TableField(fill = FieldFill.INSERT)
    private Date gmtCreate;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date gmtModified;


    public BookRepresentation convertToBookRepresentation() {
        BookRepresentation bookRepresentation = new BookRepresentation();
        BeanUtils.copyProperties(this, bookRepresentation);
        return bookRepresentation;
    }

    public Map<String, String> getBookContentMap() {
        int theBookId = this.bookId;
        List<String> chapterList = new ArrayList<>();
        Map<String, String> BookContentMap = new LinkedHashMap<String, String>();
        String encoding = "GBK";
        String thePath = "classpath:static/books/"+ this.getBookId() + '/';
        try {
            File chapterFile = ResourceUtils.getFile(thePath + theBookId + ".txt");
            if (chapterFile.isFile() && chapterFile.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(chapterFile), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String theChapter = null;
                while ((theChapter = bufferedReader.readLine()) != null) {
                    chapterList.add(theChapter);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件1！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (String theChapter : chapterList) {
            /*开始本章内容读取*/
            try {
                String allContent = "";
                File theContentFile = ResourceUtils.getFile(thePath + theChapter + ".txt");
                if (theContentFile.isFile() && theContentFile.exists()) {
                    String theContent = "";
                    BufferedReader bre = new BufferedReader(new InputStreamReader(new FileInputStream(theContentFile),encoding));
                    while ((theContent = bre.readLine()) != null) // 判断最后一行不存在，为空结束循环
                    {
                        allContent = allContent + "$$" + theContent;
                    }
                    BookContentMap.put(theChapter, allContent);
                    System.out.println(allContent);
                    bre.close();

                } else {
                    System.out.println("找不到指定的文件！2");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            /*结束本章内容读取*/
        }
        return BookContentMap;
    }
}
